﻿namespace Chain.Common
{
    using System;
    using System.Data;
    using System.Reflection;
    using System.Text;

    public class JsonPlus
    {
        public static DataTable DoubleAuotesEscape(DataTable dt)
        {
            if ((dt != null) && (dt.Rows.Count > 0))
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        if (dt.Rows[i][j] != null)
                        {
                            try
                            {
                                dt.Rows[i][j] = StringFormat(dt.Rows[i][j].ToString(), dt.Columns[j].DataType);
                            }
                            catch
                            {
                            }
                        }
                    }
                }
            }
            return dt;
        }

        private static string StringFormat(string str, Type type)
        {
            if (type == typeof(string))
            {
                str = StringToJson(str);
                return str;
            }
            if (type != typeof(DateTime))
            {
                Type type1 = typeof(bool);
            }
            return str;
        }

        private static string StringToJson(string s)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < s.Length; i++)
            {
                char ch = s.ToCharArray()[i];
                switch (ch)
                {
                    case '/':
                    {
                        builder.Append(@"\/");
                        continue;
                    }
                    case '\\':
                    {
                        builder.Append(@"\\");
                        continue;
                    }
                    case '\b':
                    {
                        builder.Append(@"\b");
                        continue;
                    }
                    case '\t':
                    {
                        builder.Append(@"\t");
                        continue;
                    }
                    case '\n':
                    {
                        builder.Append(@"\n");
                        continue;
                    }
                    case '\f':
                    {
                        builder.Append(@"\f");
                        continue;
                    }
                    case '\r':
                    {
                        builder.Append(@"\r");
                        continue;
                    }
                    case '"':
                    {
                        builder.Append("\\\"");
                        continue;
                    }
                }
                builder.Append(ch);
            }
            return builder.ToString();
        }

        public static string ToJson(object jsonObject)
        {
            string str = "{";
            PropertyInfo[] properties = jsonObject.GetType().GetProperties();
            for (int i = 0; i < properties.Length; i++)
            {
                string str2 = ((properties[i].GetGetMethod().Invoke(jsonObject, null) == null) ? "" : properties[i].GetGetMethod().Invoke(jsonObject, null)).ToString();
                string str3 = str;
                str = str3 + "\"" + properties[i].Name + "\":\"" + str2 + "\",";
            }
            return (str.Substring(0, str.Length - 1) + "}");
        }

        public static string ToJson(DataRow row, string fields)
        {
            if (fields == "")
            {
                foreach (DataColumn column in row.Table.Columns)
                {
                    if (fields != "")
                    {
                        fields = fields + ",";
                    }
                    fields = fields + column.ColumnName;
                }
            }
            StringBuilder builder = new StringBuilder();
            string[] strArray = fields.Split(",".ToCharArray());
            builder.Append("{");
            for (int i = 0; i < strArray.Length; i++)
            {
                if (builder.ToString() != "{")
                {
                    builder.Append(",");
                }
                builder.Append("\"" + strArray[i] + "\":\"" + StringFormat(row[strArray[i]].ToString(), row[strArray[i]].GetType()) + "\"");
            }
            builder.Append("}");
            return builder.ToString();
        }

        public static string ToJson(DataRow[] drs, string fields)
        {
            if (fields == "")
            {
                foreach (DataColumn column in drs[0].Table.Columns)
                {
                    if (fields != "")
                    {
                        fields = fields + ",";
                    }
                    fields = fields + column.ColumnName;
                }
            }
            string[] strArray = fields.Split(",".ToCharArray());
            StringBuilder builder = new StringBuilder();
            builder.Append("[");
            if (drs.Length > 0)
            {
                for (int i = 0; i < drs.Length; i++)
                {
                    builder.Append("{");
                    for (int j = 0; j < strArray.Length; j++)
                    {
                        builder.Append("\"" + strArray[j] + "\":\"" + StringFormat(drs[i][strArray[j]].ToString(), drs[i][strArray[j]].GetType()) + "\"");
                        if (j < (strArray.Length - 1))
                        {
                            builder.Append(",");
                        }
                    }
                    builder.Append("}");
                    if (i < (drs.Length - 1))
                    {
                        builder.Append(",");
                    }
                }
            }
            builder.Append("]");
            return builder.ToString();
        }

        public static string ToJson(DataTable dt, string fields)
        {
            dt = DoubleAuotesEscape(dt);
            if (fields == "")
            {
                foreach (DataColumn column in dt.Columns)
                {
                    if (fields != "")
                    {
                        fields = fields + ",";
                    }
                    fields = fields + column.ColumnName;
                }
            }
            string[] strArray = fields.Split(",".ToCharArray());
            StringBuilder builder = new StringBuilder();
            builder.Append("[");
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    builder.Append("{");
                    for (int j = 0; j < strArray.Length; j++)
                    {
                        builder.Append("\"" + strArray[j] + "\":\"" + dt.Rows[i][strArray[j]].ToString() + "\"");
                        if (j < (strArray.Length - 1))
                        {
                            builder.Append(",");
                        }
                    }
                    builder.Append("}");
                    if (i < (dt.Rows.Count - 1))
                    {
                        builder.Append(",");
                    }
                }
            }
            builder.Append("]");
            return builder.ToString();
        }
    }
}

